<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
// import { type FormInstance } from "element-plus"
import { type FormInstance, ElMessage, ElMessageBox } from "element-plus"
import { Plus } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { getCarList, updateCarList, saveCarList, deleteCarList } from "@/api/baseData"
const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
//#region 增

//#endregion

//#region 查
const tableData = ref<any[]>([])
const searchData = reactive({
  driverName: "",
  phoneNumber: "",
  licensePlate: ""
})
//时间选择器
const getTableData = () => {
  loading.value = true
  getCarList({
    curPage: paginationData.currentPage,
    limit: paginationData.pageSize,
    ...searchData
  })
    .then((res: any) => {
      paginationData.total = res.data.totalCount
      tableData.value = res.data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}
//#endregion
// 弹窗
const form = ref({
  id: "",
  driverName: "",
  phoneNumber: "",
  licensePlate: "",
  idCard: ""
})
const rules = {
  driverName: [
    { required: true, message: "请输入司机名", trigger: "blur" },
    { min: 1, max: 10, message: "长度 1 到 10", trigger: "blur" }
  ]
}
const dialogVisible = ref<boolean>(false)
const formRef = ref()
const title = ref<string>("新增")
const handleCreate = () => {
  dialogVisible.value = true
  title.value = "新增"
  formRef.value?.resetFields()
}
const handleUpdate = (row: any) => {
  dialogVisible.value = true
  title.value = "修改"
  form.value = row
  console.log(row)
}
const handleSave = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      if (title.value === "新增") {
        saveCarList(form.value).then(() => {
          ElMessage.success("新增成功")
          dialogVisible.value = false
          getTableData()
        })
      } else {
        updateCarList(form.value).then(() => {
          ElMessage.success("修改成功")
          dialogVisible.value = false
          getTableData()
        })
      }
    }
  })
}
const handleDelete = (row: any) => {
  ElMessageBox.confirm("确定要删除吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    deleteCarList({ id: row.id }).then(() => {
      ElMessage.success("删除成功")
      getTableData()
    })
  })
}
/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <div shadow="never" class="search-wrapper">
      <el-row class="searchForm" :gutter="20">
        <el-col :xs="2" :sm="2" :md="3" :lg="3" :xl="3">
          <el-input placeholder="司机名" v-model="searchData.driverName" style="width: auto" clearable />
        </el-col>
        <el-col :xs="2" :sm="2" :md="3" :lg="3" :xl="3">
          <el-input placeholder="手机号" v-model="searchData.phoneNumber" style="width: auto" clearable />
        </el-col>
        <el-col :xs="2" :sm="2" :md="3" :lg="3" :xl="3">
          <el-input placeholder="车牌号" v-model="searchData.licensePlate" style="width: auto" clearable />
        </el-col>
        <el-col :xs="10" :sm="10" :md="9" :lg="8" :xl="6">
          <el-button type="primary" :icon="Plus" @click="handleSearch">查询</el-button>
          <el-button type="primary" :icon="Plus" @click="handleCreate">新增</el-button>
        </el-col>
      </el-row>
    </div>
    <div v-loading="loading" shadow="never">
      <div class="table-wrapper">
        <el-table
          :data="tableData"
          style="--el-table-border-color: none"
          :header-cell-style="{
            background: '#fff !important',
            color: '#000000d9',
            'line-hight': '50px',
            'text-align': 'left',
            'font-weight': '500'
          }"
          :cell-style="{
            background: '#f7f8fb !important',
            color: '#000000d9',
            // 'line-hight': '50px',
            'text-align': 'left',
            'font-weight': '500',
            'border-bottom': '12px solid #fff'
          }"
        >
          <template #empty>
            <el-empty description="暂无数据" :image-size="80" />
          </template>
          <el-table-column label="司机名" align="center" prop="driverName" show-overflow-tooltip />
          <el-table-column label="手机号" align="center" prop="phoneNumber" show-overflow-tooltip />

          <el-table-column label="身份证号" align="center" prop="idCard" show-overflow-tooltip />
          <el-table-column label="车牌号" align="center" prop="licensePlate" show-overflow-tooltip />
          <el-table-column label="创建时间" align="center" prop="createTime" show-overflow-tooltip />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <el-button link @click="handleUpdate(scope.row)">编辑</el-button>
              <el-button link @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog v-model="dialogVisible" :title="title" width="500">
      <el-form ref="formRef" style="max-width: 600px" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="司机名" prop="driverName">
          <el-input v-model="form.driverName" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" />
        </el-form-item>
        <el-form-item label="车牌号" prop="licensePlate">
          <el-input v-model="form.licensePlate" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  height: 100%;
}
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
